/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.ProductInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.config.Config;
/*     */ import com.newcrm.db.CompanyExtManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class AddUser extends AbstractAction
/*     */ {
/*  35 */   private static Logger logger = Logger.getLogger(AddUser.class.getName());
/*     */ 
/*  37 */   private String path = "";
/*     */ 
/*     */   public AddUser(AbstractHttpServlet servlet) {
/*  40 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public AddUser(AbstractHttpServlet servlet, String path) {
/*  44 */     super(servlet);
/*  45 */     this.path = path;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  53 */     User user = super.getLoginUser(request);
/*     */ 
/*  56 */     if ((!user.isAdmin) && (!user.isGroupAdmin)) {
/*  57 */       request.getRequestDispatcher("/nopermission.jsp").forward(
/*  58 */         request, response);
/*  59 */       return;
/*     */     }
/*  61 */     UserManager manager = new UserManager();
/*  62 */     ProductInfo productInfo = Config.getInstance().productInfo;
/*     */ 
/*  65 */     if ("1".equals(productInfo.activeCodeFlag))
/*     */     {
/*  67 */       CompanyExtManager companyExtManager = new CompanyExtManager();
/*  68 */       String licenceNum = companyExtManager.getFieldValue(user.companyId, 
/*  69 */         17);
/*  70 */       int userNum = manager.getCompanyUsersNum(user.companyId);
/*     */ 
/*  72 */       if (userNum >= Integer.parseInt(licenceNum)) {
/*  73 */         logger.warn("userNum > licenceNum, userNum: " + userNum + 
/*  74 */           " / licenceNum: " + licenceNum);
/*  75 */         return;
/*     */       }
/*     */     }
/*     */ 
/*  79 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  80 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  81 */     upload.setHeaderEncoding("utf-8");
    List fileList;
/*     */     try
/*     */     {
/*  84 */       fileList = upload.parseRequest(request);
/*     */     }
/*     */     catch (FileUploadException e1)
/*     */     {
/*  86 */       e1.printStackTrace();
/*     */       return;
/*     */     }
/*  90 */     Iterator it = fileList.iterator();
/*  91 */     String userId = "";
/*  92 */     String name = "";
/*  93 */     String password = "";
/*  94 */     String comfirmPassword = "";
/*  95 */     String email = "";
/*  96 */     String imgPath = "";
/*  97 */     String maxCompanyNum = "";
/*  98 */     Date hireDate = new Date();
/*  99 */     int sex = 1;
/* 100 */     String idCard = "";
/* 101 */     String mobile = "";
/* 102 */     String qq = "";
/*     */ 
/* 104 */     int groupId = 0;
/* 105 */     int isGroupAdmin = 0;
/*     */ 
/* 107 */     FileItem fileItem = null;
/*     */ 
/* 109 */     while (it.hasNext()) {
/* 110 */       FileItem item = (FileItem)it.next();
/* 111 */       if (item.isFormField()) {
/* 112 */         String id = item.getFieldName();
/*     */ 
/* 114 */         if ("identity_name".equals(id)) {
/* 115 */           name = Util.getURLParameter(item.getString());
/*     */         }
/* 119 */         else if ("identity_userId".equals(id)) {
/* 120 */           userId = Util.getURLParameter(item.getString());
/*     */ 
/* 122 */           if (!Util.isDigital(userId)) {
/* 123 */             request.getRequestDispatcher("/errorpage.jsp").forward(
/* 124 */               request, response);
/*     */           }
/*     */ 
/*     */         }
/* 130 */         else if ("identity_group".equals(id)) {
/* 131 */           groupId = Util.getURLIntParameter(item.getString());
/*     */ 
/* 133 */           if (!user.isAdmin) {
/* 134 */             if (groupId <= 0) {
/* 135 */               request.getRequestDispatcher(
/* 136 */                 "/nopermission.jsp").forward(request, 
/* 137 */                 response);
/* 138 */               return;
/*     */             }
/*     */ 
/* 142 */             CompanyInfo company = super.getCompany(request);
/* 143 */             if (!company.isGroupAdmin(user, groupId)) {
/* 144 */               request.getRequestDispatcher(
/* 145 */                 "/nopermission.jsp").forward(request, 
/* 146 */                 response);
/*     */             }
/*     */ 
/*     */           }
/*     */ 
/*     */         }
/* 154 */         else if ("identity_isGroupAdmin".equals(id)) {
/* 155 */           isGroupAdmin = Util.getURLIntParameter(item.getString());
/*     */         }
/* 160 */         else if ("identity_sex".equals(id)) {
/* 161 */           sex = Util.getURLIntParameter(item.getString());
/*     */         }
/* 165 */         else if ("identity_hireDate".equals(id)) {
/* 166 */           hireDate = Util.str2Date(Util.getURLParameter(item
/* 167 */             .getString()));
/*     */         }
/* 171 */         else if ("identity_id".equals(id)) {
/* 172 */           idCard = Util.getURLParameter(item.getString());
/*     */         }
/* 176 */         else if ("identity_mobile".equals(id)) {
/* 177 */           mobile = Util.getURLParameter(item.getString());
/*     */         }
/* 181 */         else if ("identity_qq".equals(id)) {
/* 182 */           qq = Util.getURLParameter(item.getString());
/*     */         }
/* 186 */         else if ("identity_email".equals(id)) {
/* 187 */           email = Util.getURLParameter(item.getString());
/*     */         }
/* 191 */         else if ("maxcompany_num".equals(id)) {
/* 192 */           maxCompanyNum = Util.getURLParameter(item.getString());
/*     */         }
/* 196 */         else if ("identity_password".equals(id)) {
/* 197 */           password = Util.getURLParameter(item.getString());
/*     */         }
/* 200 */         else if ("identity_password_confirmation".equals(id)) {
/* 201 */           comfirmPassword = Util.getURLParameter(item.getString());
/*     */         }
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 207 */         String fileName = item.getName();
/* 208 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/* 212 */           fileItem = item;
/*     */         }
/*     */       }
/*     */     }
/* 215 */     if ((password != null) && (!password.equals(comfirmPassword)))
/*     */     {
/* 217 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/* 218 */         response);
/* 219 */       return;
/*     */     }
/*     */ 
/* 223 */     if (fileItem != null) {
/* 224 */       String fileName = fileItem.getName();
/* 225 */       String extName = "";
/* 226 */       if (fileName.lastIndexOf(".") >= 0) {
/* 227 */         extName = fileName.substring(fileName.lastIndexOf("."));
/*     */       }
/*     */ 
/* 230 */       if (!Util.isImageFile(extName, fileItem.get())) {
/* 231 */         request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/* 232 */           response);
/* 233 */         return;
/*     */       }
/*     */ 
/* 236 */       extName = extName.toLowerCase();
/* 237 */       imgPath = userId + extName;
/* 238 */       File file = new File(this.path, imgPath);
/*     */       try {
/* 240 */         fileItem.write(file);
/*     */       }
/*     */       catch (Exception localException)
/*     */       {
/*     */       }
/*     */     }
/* 246 */     if (groupId == 0) {
/* 247 */       isGroupAdmin = 0;
/*     */     }
/*     */     try
/*     */     {
/* 251 */       int companyId = super.getLoginUser(request).companyId;
/* 252 */       int result = manager.add(userId, name, groupId, isGroupAdmin, sex, 
/* 253 */         hireDate, idCard, mobile, qq, email, imgPath, password, 
/* 254 */         companyId);
/*     */ 
/* 256 */       if (result == 1)
/*     */       {
/* 258 */         manager.updateExtInfo(userId, 2, 
/* 259 */           maxCompanyNum);
/* 260 */         CompanyInfo company = super.getCompany(companyId);
/* 261 */         company.users = manager.getCompanyUsers(companyId, null);
/*     */       }
/*     */     } catch (DBException e) {
/* 264 */       logger.warn("add user failed ", e);
/*     */     } catch (SQLException e) {
/* 266 */       logger.warn("add user failed ", e);
/*     */     }
/* 268 */     response.sendRedirect(request.getContextPath() + "/users?groupId=" + 
/* 269 */       groupId);
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.AddUser
 * JD-Core Version:    0.6.1
 */